# 编写一个函数来查找字符串数组中的最长公共前缀。

# 如果不存在公共前缀，返回空字符串 ""。

# 示例 1:

# 输入: ["flower","flow","flight"]
# 输出: "fl"
# 示例 2:

# 输入: ["dog","racecar","car"]
# 输出: ""
# 解释: 输入不存在公共前缀。
# 说明:

# 所有输入只包含小写字母 a-z 。
from typing import *


class Solution:
    def longestCommonPrefix_my(self, strs: List[str]) -> str:
        common = []
        run = True
        for i in range(len(strs[0])):
            same = strs[0][i]
            for s in strs:
                if i >= len(s) or same != s[i]:
                    run = False
                    break
            else:
                common.append(same)
            if not run:
                break
        return "".join(common)

    def longestCommonPrefix(self, strs: List[str]) -> str:
        """别人的算法，多简洁。。。
        """
        max = ''
        for i in zip(*strs):
            if len(set(i)) == 1:
                max +=i[0]
            else:
                return max
        return max


def test_longestCommonPrefix():
    assert Solution().longestCommonPrefix(["flower", "flow", "flight"]) == "fl"
    assert Solution().longestCommonPrefix(["fl", "flow", "flight"]) == "fl"
    assert Solution().longestCommonPrefix(["dog", "car"]) == ""
    assert Solution().longestCommonPrefix(["dog", "dog", "dog"]) == "dog"
    assert Solution().longestCommonPrefix(["dog11", "dog", "dog"]) == "dog"
